/**
 * file
 */
package com.tdkj.action.front.member.seller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.newShop.ShopCommodity;
import com.tdkj.model.newShop.ShopCommodityType;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.member.seller.ICommodityManagementService;
import com.tdkj.util.PublicConfing;

/**

 * FileName:     ReleaseOfGoodsAction.java

 * @Description: TODO 发布商品

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-24 下午09:07:23

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-24       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class ReleaseOfGoodsAction extends BaseAction {

	private static final long serialVersionUID = -7414513252094322268L;
	//分类路径名
	private String classTypeJSON;
	private String classPathName;
	private String classPathId;
	
	private BeanUser user; 
	private ShopCommodity commodity ;
	private ICommodityManagementService commodityManagementService;
	
	
	
	@Override
	public String execute() throws Exception {
		//List<ShopCommodityType> list = commodityManagementService.findAll(ShopCommodityType.class);
		List<ShopCommodityType> list = commodityManagementService.findByProperty("level", 1, ShopCommodityType.class);

		// 没有数据时，添加商品分类，测试数据 真实部署时删除以下if语句
		if (list.size() < 1) {
			initShopCommodityType();
			list = commodityManagementService.findByProperty("level", 1, ShopCommodityType.class);
		}
		// 测试数据 结束

		if (list.size() > 0) {

			JsonConfig config = new JsonConfig();
			config.setIgnoreDefaultExcludes(false);
			config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			config.setExcludes(new String[] {// 只要设置这个数组，指定过滤哪些字段。
					"shopCommoditys", "parent" });
			classTypeJSON = JSONSerializer.toJSON(list, config).toString();
			System.out.println(JSONSerializer.toJSON(list, config).toString());

			// JSONArray jsons=JSONArray.fromObject(list);
			// System.out.println(jsons);
		}
		return super.execute();
	}
	
	private void initShopCommodityType() {
		ShopCommodityType 有色金属 = new ShopCommodityType();
		有色金属.setName("有色金属");
		有色金属.setDescription("有色金属");
		有色金属.setLevel(1);
		commodityManagementService.save(有色金属);

		ShopCommodityType 黑色金属 = new ShopCommodityType();
		黑色金属.setName("黑色金属");
		黑色金属.setDescription("黑色金属");
		黑色金属.setLevel(1);
		commodityManagementService.save(黑色金属);

		ShopCommodityType 非金属矿产 = new ShopCommodityType();
		非金属矿产.setName("非金属矿产");
		非金属矿产.setDescription("非金属矿产");
		非金属矿产.setLevel(1);
		commodityManagementService.save(非金属矿产);

		ShopCommodityType 废金属 = new ShopCommodityType();
		废金属.setName("废金属");
		废金属.setDescription("废金属");
		废金属.setLevel(1);
		commodityManagementService.save(废金属);

		ShopCommodityType 水气矿产 = new ShopCommodityType();
		水气矿产.setName("水气矿产");
		水气矿产.setDescription("水气矿产");
		水气矿产.setLevel(1);
		commodityManagementService.save(水气矿产);

		ShopCommodityType 矿业设备及材料 = new ShopCommodityType();
		矿业设备及材料.setName("矿业设备及材料");
		矿业设备及材料.setDescription("矿业设备及材料");
		矿业设备及材料.setLevel(1);
		commodityManagementService.save(矿业设备及材料);

		ShopCommodityType 矿业药剂 = new ShopCommodityType();
		矿业药剂.setName("矿业药剂");
		矿业药剂.setDescription("矿业药剂");
		矿业药剂.setLevel(1);
		commodityManagementService.save(矿业药剂);

		ShopCommodityType 矿业书籍 = new ShopCommodityType();
		矿业书籍.setName("矿业书籍");
		矿业书籍.setDescription("矿业书籍");
		矿业书籍.setLevel(1);
		commodityManagementService.save(矿业书籍);

		ShopCommodityType 办公用品 = new ShopCommodityType();
		办公用品.setName("办公用品");
		办公用品.setDescription("办公用品");
		办公用品.setLevel(1);
		commodityManagementService.save(办公用品);

		ShopCommodityType 钢材相关 = new ShopCommodityType();
		钢材相关.setName("钢材相关 ");
		钢材相关.setDescription("钢材相关");
		钢材相关.setLevel(1);
		commodityManagementService.save(钢材相关);

		ShopCommodityType 五金相关 = new ShopCommodityType();
		五金相关.setName("五金相关");
		五金相关.setDescription("五金相关");
		五金相关.setLevel(1);
		commodityManagementService.save(五金相关);

		ShopCommodityType 矿用工具 = new ShopCommodityType();
		矿用工具.setName("矿用工具");
		矿用工具.setDescription("矿用工具");
		矿用工具.setLevel(1);
		commodityManagementService.save(矿用工具);

		ShopCommodityType 设计制作 = new ShopCommodityType();
		设计制作.setName("设计制作");
		设计制作.setDescription("设计制作");
		设计制作.setLevel(1);
		commodityManagementService.save(设计制作);
		// ------------------------------2-------------------------------------------
		ShopCommodityType 重有色 = new ShopCommodityType();
		重有色.setName("重有色");
		重有色.setDescription("重有色");
		重有色.setParent(有色金属);
		commodityManagementService.save(重有色);

		ShopCommodityType 轻有色 = new ShopCommodityType();
		轻有色.setName("轻有色 ");
		轻有色.setDescription("轻有色");
		重有色.setParent(有色金属);
		commodityManagementService.save(轻有色);

		ShopCommodityType 贵金属 = new ShopCommodityType();
		贵金属.setName("贵金属");
		贵金属.setDescription("贵金属");
		贵金属.setParent(有色金属);
		commodityManagementService.save(贵金属);

		ShopCommodityType 半金属 = new ShopCommodityType();
		半金属.setName("半金属");
		半金属.setDescription("半金属");
		半金属.setParent(有色金属);
		commodityManagementService.save(半金属);

		ShopCommodityType 稀土金属 = new ShopCommodityType();
		稀土金属.setName("稀土金属");
		稀土金属.setDescription("稀土金属");
		稀土金属.setParent(有色金属);
		commodityManagementService.save(稀土金属);

		ShopCommodityType 稀有轻 = new ShopCommodityType();
		稀有轻.setName("稀有轻");
		稀有轻.setDescription("稀有轻");
		稀有轻.setParent(有色金属);
		commodityManagementService.save(稀有轻);

		ShopCommodityType 高熔点金属 = new ShopCommodityType();
		高熔点金属.setName("高熔点金属");
		高熔点金属.setDescription("高熔点金属");
		高熔点金属.setParent(有色金属);
		commodityManagementService.save(高熔点金属);

		ShopCommodityType 放射性金属 = new ShopCommodityType();
		放射性金属.setName("放射性金属");
		放射性金属.setDescription("放射性金属");
		放射性金属.setParent(有色金属);
		commodityManagementService.save(放射性金属);

		ShopCommodityType 稀散金属 = new ShopCommodityType();
		稀散金属.setName("稀散金属");
		稀散金属.setDescription("稀散金属");
		稀散金属.setParent(有色金属);
		commodityManagementService.save(稀散金属);

		Set<ShopCommodityType> sets = new HashSet<ShopCommodityType>();
		sets.add(重有色);
		sets.add(轻有色);
		sets.add(贵金属);
		sets.add(半金属);
		sets.add(稀土金属);
		sets.add(稀有轻);
		sets.add(高熔点金属);
		sets.add(放射性金属);
		sets.add(稀散金属);
		有色金属.setChils(sets);
		// commodityManagementService.merge(sets);
		commodityManagementService.save(有色金属);
		
		BeanUser user= (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		ShopCommodity shopCommodity1=new ShopCommodity();
		shopCommodity1.setBeanUser(user);
		shopCommodity1.setCommodityType(重有色);
		shopCommodity1.setSaleType(0);
		shopCommodity1.setBabyType(1);
		shopCommodity1.setCommodityTitle("测试商品标题");
		shopCommodity1.setPrice(100.03);
		shopCommodity1.setBusinessesEncoding("CKLDJSLMIOSNOIVDNSIONVOISD");
		shopCommodity1.setCommodityNumber(100000);
		shopCommodity1.setGoodsPictures("http://XXXX");
		shopCommodity1.setFloatingColor("#405495");
		shopCommodity1.setFloatingTime("2012-2-2 12:12:34:34");
		shopCommodity1.setKeyword("关键字1 关键字2 关键字3");
		shopCommodity1.setDetailedDescription("这是详细描述");
		shopCommodity1.setStartingPrice(234324.00);
		shopCommodity1.setLowestFare(123213.23);
		shopCommodity1.setLocation("云南省昆明");
		shopCommodity1.setFreightTypes(0);
		shopCommodity1.setSurfacePrice(12.0);
		shopCommodity1.setExpressPrice(232.0);
		shopCommodity1.setEmsPrice(123.00);
		shopCommodity1.setEffective(4);
		shopCommodity1.setInvoice(1);
		shopCommodity1.setWarranty(1);
		shopCommodity1.setAutomaticRepeat(0);
		commodityManagementService.save(shopCommodity1);
		
	}

	public String getClassTypeJSON() {
		return classTypeJSON;
	}

	public void setClassTypeJSON(String classTypeJSON) {
		this.classTypeJSON = classTypeJSON;
	}
	
	// 发布商品详细页面
	public String postGoods() {
		return "postGoods";
	}
	
	/**
	 *	发布商品 拍卖 
	 */
	public String postGoodsAuction(){
		Boolean isPostSuccess = true;
		if(commodity ==  null){
			return "postGoodsAuction";
		}
		
		user = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		if(user == null){
			return "postGoodsAuction";
		}else{
			commodity.setBeanUser(user);
		}
		
		if(StringUtils.isEmpty(commodity.getCommodityTitle())){
			this.addFieldError("commodity.commodityTitle", "商品标题不能为空!");	
			isPostSuccess = false;
		}
		
		if(isPostSuccess){
			commodityManagementService.save(commodity);	
		}
		return "postGoodsAuction";
	}
	
	/**
	 *	发布商品 定价
	 */	
	public String postGoodsFixedPrice(){
		Boolean isPostSuccess = true;
		if(commodity ==  null){
			return "postGoodsFixedPrice";
		}
		
		user = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		if(user == null){
			return "postGoodsFixedPrice";
		}else{
			commodity.setBeanUser(user);
		}
		
		if(StringUtils.isEmpty(commodity.getCommodityTitle())){
			this.addFieldError("commodity.commodityTitle", "商品标题不能为空!");	
			isPostSuccess = false;
		}
		
		if(isPostSuccess){
			commodityManagementService.save(commodity);	
		}
		return "postGoodsFixedPrice";
	}
	
	public String getClassPathName(){
		return classPathName;
	}
	
	public void setClassPathName(String classPathName){
		try {
			this.classPathName = URLDecoder.decode( classPathName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getClassPathId(){
		return classPathId;
	}
	
	public void setClassPathId(String classPathId){
		this.classPathId = classPathId;
	}	
	
	public ShopCommodity getCommodity() {
		return commodity;
	}

	public void setCommodity(ShopCommodity commodity) {
		this.commodity = commodity;
	}
	
	public ICommodityManagementService getCommodityManagementService() {
		return commodityManagementService;
	}

	@Resource
	public void setCommodityManagementService(ICommodityManagementService commodityManagementService) {
		this.commodityManagementService = commodityManagementService;
	}

	public BeanUser getUser() {
		return user;
	}

	public void setUser(BeanUser user) {
		this.user = user;
	}

	

}
